﻿using FineUI;
using System;
using System.Linq;
using System.Text;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.BranchActivity
{
    public partial class ApprovalBranchActivitie : PageBase
    {
        #region 页面属性

        public string RelationID
        {
            get
            {
                return this.Request["RelationID"] ?? "";
            }
        }

        public string TaskID
        {
            get
            {
                return this.Request["TaskID"] ?? "";
            }
        }

        public string TaskType
        {
            get
            {
                return this.Request["TaskType"] ?? "";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(this.RelationID))
            {
                PartyBranchActivitie oModel = DB.PartyBranchActivities.Find(RelationID);
                lbTitle.Text = oModel.Title;
                lbTime.Text = Convert.ToString(oModel.BeginTime) + "-" + Convert.ToString(oModel.EndTime);
                lbAddress.Text = oModel.Address;

                IQueryable<PartyBranchActivitieMember> q = DB.PartyBranchActivitieMembers.Where(t => t.ActivitieID == RelationID);

                if (q.Count() > 0)
                {
                    StringBuilder sbLoginID = new StringBuilder();
                    StringBuilder sbName = new StringBuilder();

                    foreach (PartyBranchActivitieMember item in q)
                    {
                        sbLoginID.Append(item.LoginID + ",");
                        sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                    }

                    lbParticipant.Text = sbName.ToString().TrimEnd(',');
                }

                lbContent.Text = oModel.Summary;

                if (oModel.IsEnd == true)
                {
                    //活动结束
                    this.btnConfirm.Hidden = true;
                    this.btnLeave.Hidden = true;
                    this.btnSave.Hidden = true;
                    this.lbTip.Text = "我的活动（活动已结束）";
                }
                else
                {
                    this.btnConfirm.Hidden = false;
                    this.btnLeave.Hidden = false;
                    this.btnSave.Hidden = false;
                    this.lbTip.Text = "我的活动";
                }

                if (TaskType == "1")//是否待办
                {
                    this.btnConfirm.Hidden = false;
                    this.btnLeave.Hidden = false;
                    this.btnSave.Hidden = false;
                }
                else
                {
                    this.btnConfirm.Hidden = true;
                    this.btnLeave.Hidden = true;
                    this.btnSave.Hidden = true;
                }
            }

        }

        #region 按钮事件
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            PartyBranchActivitieMember.UpdateMemberStatus(this.RelationID, this.CurrentUser.LoginID, 2);
            PartyTaskHelper.TaskDoneByTaskId(this.TaskID, this.CurrentUser.LoginID);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnLeave_Click(object sender, EventArgs e)
        {
            PartyBranchActivitieMember.UpdateMemberStatus(this.RelationID, this.CurrentUser.LoginID, 3);
            PartyTaskHelper.TaskDoneByTaskId(this.TaskID, this.CurrentUser.LoginID);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PartyTaskHelper.TaskDoneByTaskId(this.TaskID, this.CurrentUser.LoginID);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}